<template>
  <div class="container">
    <div class="app-container">
      <!-- 角色管理内容 -->
      <div class="role-operate">
        <el-button size="mini" type="primary" @click="showDialog = true">添加角色</el-button>
      </div>
      <!-- 放置table组件 -->
      <el-table v-loading="loading" :data="list">
        <!-- 放置列 -->
        <el-table-column prop="name" align="center" width="200" label="角色">
          <template #default="{row}">
            <el-input v-if="row.isEdit" v-model="row.editRow.name" />

            <span v-else>{{ row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="state" align="center" width="200" label="启用">
          <template #default="{row}">
            <el-switch
              v-if="row.isEdit"
              v-model="row.editRow.state"
              :active-value="1"
              :inactive-value="0"
            />

            <span v-else>{{ row.state ? '已启用':'未启用' }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="description" align="center" label="描述">
          <template v-slot="{ row }">
            <el-input v-if="row.isEdit" v-model="row.editRow.description" type="textarea" />
            <span v-else>{{ row.description }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template #default="{row}">
            <template v-if="row.isEdit">
              <!-- 编辑状态 -->
              <el-button type="primary" size="mini" @click="submit(row)">确定</el-button>
              <el-button size="mini" @click="row.isEdit = false">取消</el-button>
            </template>

            <template v-else>
              <el-button size="mini" type="text">分配权限</el-button>
              <el-button size="mini" type="text" @click="setRole(row)">编辑</el-button>
              <el-popconfirm
                title="这是一段内容确定删除吗？"
                @onConfirm="delRole(row.id)"
              >
                <el-button
                  slot="reference"
                  size="mini"
                  type="text"
                  style="marginLeft:10px"
                >删除</el-button>
              </el-popconfirm>
            </template>
          </template>
        </el-table-column>
      </el-table>
      <!-- 放置分页组件 -->
      <el-row type="flex" style="height:60px" align="middle" justify="end">
        <!-- 放置分页组件 -->
        <el-pagination
          :current-page="queryObj.page"
          :page-sizes="[1,3,5,10]"
          :page-size="queryObj.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </el-row>
    </div>

    <!-- dialog弹窗 -->
    <el-dialog width="500px" title="新增角色" :visible.sync="showDialog" @close="closeDialog">
      <!-- 表单内容 -->
      <el-form ref="form" label-width="120px" :model="form" :rules="rules">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="form.name" style="width:300px" size="mini" />
        </el-form-item>
        <el-form-item label="启用">
          <el-switch
            v-model="form.state"
            size="mini"
            :active-value="1"
            :inactive-value="0"
          />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="form.description" type="textarea" :rows="3" style="width:300px" size="mini" />
        </el-form-item>
        <el-form-item>
          <el-row type="flex" justify="center">
            <el-col :span="12">
              <el-button type="primary" size="mini" @click="addRole">确定</el-button>
              <el-button size="mini" @click="closeDialog">取消</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>

  </div>
</template>
<script>
import { getRoleList, addRole, updateRole, removeRole } from '@/api/role'

export default {
  name: 'Role',
  data () {
    return {
      loading: false,
      queryObj: {
        page: 1,
        pagesize: 5
      },
      list: [], // table的数据源
      total: null,
      showDialog: false,
      form: {
        name: '',
        description: '',
        state: 0
      },
      rules: {
        name: [{ required: true, message: '请输入角色名称', trigger: 'blur' }],
        description: [{ required: true, message: '请输入角色名称', trigger: 'blur' }]
      }
    }
  },

  created () {
    this.getRoleData()
  },

  methods: {
    // TODO:分配权限请求
    setRole (row) {
      console.log(row)
      row.isEdit = true
    },

    // 获取table数据
    async getRoleData () {
      this.loading = true

      const data = await getRoleList(this.queryObj)

      this.list = [...data.rows]

      this.list.forEach(item => {
        // item.isEdit = false // 添加一个属性 初始值为false
        // 数据响应式的问题  数据变化 视图更新
        // 添加的动态属性 不具备响应式特点
        // this.$set(目标对象, 属性名称, 初始值) 可以针对目标对象 添加的属性 添加响应式
        this.$set(item, 'isEdit', false)
        this.$set(item, 'editRow', {
          name: item.name,
          description: item.description,
          state: item.state
        })
      })

      this.total = data.total

      this.loading = false
    },

    // 每页数量变化时触发
    handleSizeChange (pageSize) {
      // console.log(pageSize)

      this.queryObj.pagesize = pageSize

      this.getRoleData()
    },

    // 当前页变化时触发
    handleCurrentChange (current) {
      // console.log(current)

      this.queryObj.page = current

      this.getRoleData()
    },

    // 添加角色
    async addRole () {
      try {
        await this.$refs.form.validate()

        await addRole(this.form)

        this.$message.success('添加成功')

        this.closeDialog()

        this.getRoleData()
      } catch (error) {
        console.log(error)
      }
    },

    // 关闭弹窗
    closeDialog () {
      // 重置表单
      this.$refs.form.resetFields()

      // 关闭弹窗
      this.showDialog = false
    },

    // 更新角色信息
    async submit (row) {
      try {
        if (row.editRow.name && row.editRow.description) {
          // console.log(row.editRow)
          await updateRole({ ...row.editRow, id: row.id })

          this.$message.success('修改成功')

          this.getRoleData()
        } else {
          this.$message.error('请输入必填项')
        }
      } catch (error) {
        console.log(error)
      }
    },

    // 删除角色
    async delRole (id) {
      await removeRole(id)

      if (this.list.length === 1) this.queryObj.page--

      this.getRoleData()
    }
  }

}
</script>

<style scoped>
.role-operate {
  padding: 10px;
}
</style>
